<template>

  <el-col :span="7"
          class="personal">
    <el-form ref="form"
             :model="form"
             label-width="80px"
             label-position='left'>
      <el-form-item label="姓名:">
        <el-input v-model="form.name"
                  :disabled='disabled'></el-input>
      </el-form-item>
      <el-form-item label="工号:">
        <el-input v-model="form.number"
                  disabled></el-input>
      </el-form-item>
      <el-form-item label="手机号:">
        <el-input v-model="form.phone"
                  :disabled='disabled'></el-input>
      </el-form-item>
      <el-form-item label="所属门店:">
        <el-input v-model="form.Stores"
                  :disabled='disabled'></el-input>
      </el-form-item>
      <el-form-item label="地址:">
        <el-input v-model="form.address"
                  :disabled='disabled'></el-input>
      </el-form-item>
      <el-form-item label="头像:">
        <el-upload class="avatar-uploader"
                   @click="handlePictureCardPreview"
                   action="https://jsonplaceholder.typicode.com/posts/"
                   :show-file-list="true"
                   :on-success="handleAvatarSuccess"
                   :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl"
               :src="imageUrl"
               class="avatar">
          <i v-else
             class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%"
               :src="dialogImageUrl"
               alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button id="baocun"
                   @click="onSubmit">保存</el-button>
        <el-button @click="bianji">编辑</el-button>
      </el-form-item>
    </el-form>
  </el-col>
</template>

<script>
export default {
  data () {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      imageUrl: '',
      userlist: {},
      disabled: true,
      form: {
        name: '',
        phone: '',
        address: '',
        Stores: '',
        number: ''
      }
    }
  },
  methods: {
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //编辑
    bianji () {
      this.disabled = false
    },
    //保存
    onSubmit () {
      this.$axios({
        method: 'post',
        url: '/lky/personal/bianji',
        data: this.form
      }).then((res) => {
        console.log(res);
      })
      this.disabled = true
    },

    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }

  },
  mounted () {
    this.userlist = JSON.parse(sessionStorage.getItem('user'))
    this.form.name = this.userlist.user_name
    this.form.phone = Number(this.userlist.user_tel)
    this.form.address = this.userlist.user_address
    this.form.Stores = this.userlist.user_company
    this.form.number = Number(this.userlist.user_id)
  }
}
</script>

<style scoped>
.el-form-item__label {
  text-align: left;
}
.personal {
  padding-top: 60px;
}
#baocun {
  margin-right: 100px;
}
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 2px dashed #8c939d;
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
}
.avatar {
  width: 140px;
  height: 140px;
  display: block;
}
</style>